using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ComputaGuru.SpEAd
{
    public partial class LinkSelector : Form
    {
        private LinkSelectorMode selectorMode = LinkSelectorMode.Hide;
        private EA.Repository eaRepository = null;
        private List<LinkDescriptor> selectedLinks = new List<LinkDescriptor>();

        /// <summary>
        /// Style selected in SetStyle mode
        /// </summary>
        public LinkStyle SelectedStyle
        {
            get { return (LinkStyle)Enum.Parse(typeof(LinkStyle), this.selectedStyle.SelectedIndex.ToString()); }
        }

        /// <summary>
        /// Mode of operation for the selector
        /// </summary>
        internal LinkSelectorMode Mode
        {
            get { return selectorMode; }
            set { selectorMode = value; }
        }

        /// <summary>
        /// Collection of links selected by the user
        /// </summary>
        public List<LinkDescriptor> SelectedLinks
        {
            get { return this.selectedLinks; }
            set { this.selectedLinks = value; }
        }

        /// <summary>
        /// EA Repository to display links for
        /// </summary>
        public EA.Repository EaRepository
        {
            get { return this.eaRepository; }
            set { this.eaRepository = value; }
        }

        
        public LinkSelector()
        {
            InitializeComponent();
        }

        private void LinkSelector_Load(object sender, EventArgs e)
        {
            this.linkStyleLabel.Visible = (this.Mode == LinkSelectorMode.SetStyle);
            this.selectedStyle.Visible = (this.Mode == LinkSelectorMode.SetStyle);

            if (this.EaRepository != null)
            {
                //populate the list of links
                EA.Diagram currentDiagram = this.EaRepository.GetCurrentDiagram();
                foreach (EA.DiagramLink currentObject in currentDiagram.DiagramLinks)
                {
                    if (((this.Mode == LinkSelectorMode.Hide || this.Mode == LinkSelectorMode.SetStyle) && !currentObject.IsHidden)
                        || (this.Mode == LinkSelectorMode.Unhide && currentObject.IsHidden))
                    {
                        EA.Connector linkConnector = this.EaRepository.GetConnectorByID(currentObject.ConnectorID);
                        EA.Element sourceElement = this.EaRepository.GetElementByID(linkConnector.ClientID);
                        EA.Element targetElement = this.EaRepository.GetElementByID(linkConnector.SupplierID);
                        this.linkList.Items.Add(new LinkDescriptor(currentObject, sourceElement.Name, targetElement.Name, linkConnector.Type));
                    }
                }
            }
        }

        private void selectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.linkList.Items.Count; i++)
            {
                this.linkList.SetSelected(i, true);
            }
        }

        private void selectNone_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.linkList.Items.Count; i++)
            {
                this.linkList.SetSelected(i, false);
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            foreach (LinkDescriptor currentLink in this.linkList.SelectedItems)
            {
                this.SelectedLinks.Add(currentLink);
            }

            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }
    }
}